/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JRentFilter.java
 *
 * Created on 2009-8-3, 9:12:26
 */

package cn.ekuma.epos.rant;

import cn.ekuma.epos.bean.util.CustomerHelper;
import cn.ekuma.epos.datalogic.I_DataLogicCustomers;
import cn.ekuma.epos.datalogic.I_DataLogicSales;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.gui.ComboBoxValEntry;
import com.openbravo.data.gui.ComboBoxValModel;
import com.openbravo.data.gui.MessageInf;
import com.openbravo.data.loader.serialize.Datas;
import com.openbravo.data.loader.serialize.serializer.SerializerWrite;
import com.openbravo.data.loader.serialize.serializer.write.SerializerWriteBasic;
import com.openbravo.data.loader.query.QBFCompareEnum;

import com.openbravo.pos.base.AppLocal;
import com.openbravo.pos.base.AppView;
import com.openbravo.bean.infobean.CustomerInfo;
import com.openbravo.bean.Product;
import com.openbravo.bean.Rant;
import com.openbravo.pos.panels.JCustomerFinder;
import com.openbravo.pos.panels.JProductFinder;
import com.openbravo.pos.reports.ReportEditorCreator;

import java.awt.Component;
import java.awt.Toolkit;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author Administrator
 */
public class JRentFilter extends javax.swing.JPanel implements ReportEditorCreator {
    private I_DataLogicSales m_dlSales;
    private I_DataLogicCustomers dlCustomers;
    private Product product;
    private CustomerInfo customerInfo;
    private ComboBoxValModel rantState;

    JRentFilter(I_DataLogicSales dlSales, I_DataLogicCustomers dlCustomers1) {
        dlCustomers = dlCustomers1;
        m_dlSales = dlSales;
        initComponents();
        rantState = new ComboBoxValModel();
        rantState.add(new ComboBoxValEntry(Rant.RANTSTATE_noRant, AppLocal.getIntString("list.RANTSTATE_init")));
        rantState.add(new ComboBoxValEntry(Rant.RANTSTATE_rant, AppLocal.getIntString("list.RANTSTATE_noRant")));
        rantState.add(new ComboBoxValEntry(Rant.RANTSTATE_returned, AppLocal.getIntString("list.RANTSTATE_returned")));
        rantState.add(new ComboBoxValEntry(Rant.RANTSTATE_all, AppLocal.getIntString("list.RANTSTATE_all")));
        rantState.setSelectedKey(Rant.RANTSTATE_rant);
        jComboBox1.setModel(rantState);
    }

    /**
     * This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        m_jName1 = new javax.swing.JTextField();
        jcard1 = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        btnCustomer1 = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jComboBox1 = new javax.swing.JComboBox();
        jPanel3 = new javax.swing.JPanel();
        m_jSearch1 = new javax.swing.JTextField();
        search1 = new javax.swing.JButton();
        Enter3 = new javax.swing.JButton();
        m_jBarcode2 = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(AppLocal.getIntString("label.bycustomer"))); // NOI18N

        jLabel4.setText(AppLocal.getIntString("label.name")); // NOI18N

        m_jName1.setEditable(false);

        jcard1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcard1ActionPerformed(evt);
            }
        });

        jLabel11.setText(AppLocal.getIntString("label.card")); // NOI18N

        btnCustomer1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/kuser.png"))); // NOI18N
        btnCustomer1.setFocusPainted(false);
        btnCustomer1.setFocusable(false);
        btnCustomer1.setMargin(new java.awt.Insets(8, 14, 8, 14));
        btnCustomer1.setRequestFocusEnabled(false);
        btnCustomer1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCustomer1ActionPerformed(evt);
            }
        });

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(AppLocal.getIntString("label.rantstate"))); // NOI18N

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
                jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel4Layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jComboBox1, 0, 80, Short.MAX_VALUE)
                                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
                jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel4Layout.createSequentialGroup()
                                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap(6, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addGroup(jPanel1Layout.createSequentialGroup()
                                                .addComponent(jLabel11, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(jcard1, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addGroup(jPanel1Layout.createSequentialGroup()
                                                .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, 126, Short.MAX_VALUE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(m_jName1, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnCustomer1)
                                .addGap(18, 18, 18)
                                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(28, 28, 28))
        );
        jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(jPanel1Layout.createSequentialGroup()
                                                .addGap(1, 1, 1)
                                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                        .addComponent(jLabel11)
                                                        .addComponent(jcard1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                        .addComponent(jLabel4)
                                                        .addComponent(m_jName1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                        .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(btnCustomer1, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(AppLocal.getIntString("label.byproduct"))); // NOI18N

        m_jSearch1.setEditable(false);
        m_jSearch1.setFocusable(false);
        m_jSearch1.setRequestFocusEnabled(false);

        search1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/search.png"))); // NOI18N
        search1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                search1ActionPerformed(evt);
            }
        });

        Enter3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/apply.png"))); // NOI18N
        Enter3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Enter3ActionPerformed(evt);
            }
        });

        m_jBarcode2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                m_jBarcode2ActionPerformed(evt);
            }
        });

        jLabel10.setText(AppLocal.getIntString("label.prodbarcode")); // NOI18N

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
                jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel3Layout.createSequentialGroup()
                                .addContainerGap(41, Short.MAX_VALUE)
                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                                                .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(m_jBarcode2, javax.swing.GroupLayout.PREFERRED_SIZE, 241, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(Enter3))
                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                                                .addComponent(m_jSearch1, javax.swing.GroupLayout.PREFERRED_SIZE, 378, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(search1)))
                                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
                jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel3Layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(jPanel3Layout.createSequentialGroup()
                                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                        .addComponent(jLabel10)
                                                        .addComponent(m_jBarcode2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                                .addGap(4, 4, 4))
                                        .addComponent(Enter3))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(m_jSearch1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(search1)))
        );

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel2Layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void search1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_search1ActionPerformed
        assignProduct(JProductFinder.showMessage(this, m_dlSales, JProductFinder.PRODUCT_NORMAL));
    }//GEN-LAST:event_search1ActionPerformed

    private void Enter3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Enter3ActionPerformed
        assignProductByCode();
    }//GEN-LAST:event_Enter3ActionPerformed

    private void m_jBarcode2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_m_jBarcode2ActionPerformed
        assignProductByCode();
    }//GEN-LAST:event_m_jBarcode2ActionPerformed

    private void btnCustomer1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCustomer1ActionPerformed
        assignCustomer();
    }//GEN-LAST:event_btnCustomer1ActionPerformed

    private void jcard1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcard1ActionPerformed
        assignCustomerByCard();
    }//GEN-LAST:event_jcard1ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Enter3;
    private javax.swing.JButton btnCustomer1;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JTextField jcard1;
    private javax.swing.JTextField m_jBarcode2;
    private javax.swing.JTextField m_jName1;
    private javax.swing.JTextField m_jSearch1;
    private javax.swing.JButton search1;
    // End of variables declaration//GEN-END:variables


    private void assignProductByCode() {
        /*try {
            Product prod = m_dlSales.getProductInfoByCode(m_jBarcode2.getText());
            if (prod == null) {
                Toolkit.getDefaultToolkit().beep();
            }
            assignProduct(prod);
        } catch (BasicException eData) {
            MessageInf msg = new MessageInf(eData);
            msg.show(this);
            assignProduct(null);
        }*/
    }

    private void assignProduct(Product prod) {
        product = prod;
        if (product == null) {
            m_jSearch1.setText(null);
            m_jBarcode2.setText(null);
        } else {
            m_jSearch1.setText(product.getReference() + " - " + product.getName());
            m_jBarcode2.setText(product.getCode());
        }
    }

    private void assignCustomerByCard() {
        try {
            assignCustomer(dlCustomers.findCustomerInfoByCard(jcard1.getText(), null));
        } catch (BasicException ex) {
            Logger.getLogger(JRentFilter.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void assignCustomer() {
        JCustomerFinder customerFinder = JCustomerFinder.getCustomerFinder(this, m_dlSales);
        customerFinder.setVisible(true);
        assignCustomer(customerFinder.getSelectedCustomer());
    }

    private void assignCustomer(CustomerInfo cust) {
        this.customerInfo = cust;
        if (customerInfo == null) {
            this.jcard1.setText(null);
            this.m_jName1.setText(null);
        } else {
            this.jcard1.setText(null);
            this.m_jName1.setText(CustomerHelper.printName(customerInfo));
        }
    }

    @Override
    public void activate() throws BasicException {

    }

    @Override
    public Component getComponent() {
        return this;
    }

    @Override
    public SerializerWrite getSerializerWrite() {
        return new SerializerWriteBasic(Datas.OBJECT, Datas.STRING, Datas.OBJECT, Datas.STRING);
    }

    @Override
    public Object createValue() throws BasicException {
        String m_sRantState = rantState.getSelectedKey().toString();
        QBFCompareEnum m_SQBF = QBFCompareEnum.COMP_EQUALS;
        if (Rant.RANTSTATE_all.equalsIgnoreCase(m_sRantState)) {
            m_sRantState = null;
            m_SQBF = QBFCompareEnum.COMP_NONE;
        }
        if (this.product != null && this.customerInfo != null) {
            return new Object[]{
                    QBFCompareEnum.COMP_EQUALS, product.getID(),
                    QBFCompareEnum.COMP_EQUALS, customerInfo.getId(),
                    m_SQBF, m_sRantState
            };
        } else if (this.product != null && this.customerInfo == null) {
            return new Object[]{
                    QBFCompareEnum.COMP_EQUALS, product.getID(),
                    QBFCompareEnum.COMP_NONE, null,
                    m_SQBF, m_sRantState
            };
        } else if (this.product == null && this.customerInfo != null) {
            return new Object[]{
                    QBFCompareEnum.COMP_NONE, null,
                    QBFCompareEnum.COMP_EQUALS, customerInfo.getId(),
                    m_SQBF, m_sRantState
            };
        }
        return new Object[]{
                QBFCompareEnum.COMP_NONE, null,
                QBFCompareEnum.COMP_NONE, null,
                m_SQBF, m_sRantState
        };
    }

    public CustomerInfo getCustomerInfo() {
        return customerInfo;
    }

    public void setCustomerInfo(CustomerInfo customerInfo) {
        this.customerInfo = customerInfo;
    }
}

